package com.mingtu.jgspatrol.fragment;

import static com.mingtu.jgspatrol.utils.MyConstant.animationType;
import static com.mingtu.jgspatrol.utils.MyConstant.isFirstOnly;
import static com.mingtu.jgspatrol.utils.MyConstant.isOpenLoadAnimation;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.base.BaseBindingFragment;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.view.EmptyDataLayout;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.activity.ReportedDetailsActivity;
import com.mingtu.jgspatrol.adapter.MyRecordReportAdapter;
import com.mingtu.jgspatrol.bean.MyReportRecordBean;
import com.mingtu.jgspatrol.databinding.FragmentNav3Binding;
import com.mingtu.jgspatrol.utils.MyConstant;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class Nav3Fragment extends BaseBindingFragment<FragmentNav3Binding> implements LazyFragmentPagerAdapter.Laziable {
    
    private MyRecordReportAdapter myRecordReportAdapter;
    private int currentPage = 1;
    private boolean isLoadMore = true;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_nav3;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext().getApplicationContext());
        binding.recycler.setLayoutManager(linearLayoutManager);

        //添加自定义分割线
//        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
//        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_dive_line));
//        binding.recycler.addItemDecoration(divider);
        myRecordReportAdapter = new MyRecordReportAdapter(context);
        if (isOpenLoadAnimation) {
            myRecordReportAdapter.isFirstOnly(isFirstOnly);
            myRecordReportAdapter.openLoadAnimation(animationType);
        }
        myRecordReportAdapter.setEmptyView(new EmptyDataLayout(context));
        binding.recycler.setAdapter(myRecordReportAdapter);
        myRecordReportAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String eventId = myRecordReportAdapter.getData().get(position).getId();
                Intent intent = new Intent(getActivity(), ReportedDetailsActivity.class);
                intent.putExtra("eventId", eventId);
                intent.putExtra("sourceType", "0");
                startActivityForResult(intent, 100);
            }
        });

    }

    @Override
    protected void getData() {
        getReportList(1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == 200) {
                getReportList(1);
            }
        }
    }

    /**
     * 首页待上报的事件列表
     */
    public void getReportList(int pageNum) {
        if (pageNum == 1) {
            currentPage = 1;
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("curPage", pageNum);
        hashMap.put("pageSize", Constant.PAGE_SIZE);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_INDEX_REPORT_TASK)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                MyReportRecordBean myReportRecordBean = new MyReportRecordBean();
                                myReportRecordBean = gson.fromJson(result, MyReportRecordBean.class);
                                List<MyReportRecordBean.PageBean.ListBean> dataBeans = myReportRecordBean.getPage().getList();

                                try {
                                    if (dataBeans != null) {
                                        isLoadMore = dataBeans.size() > 0 && dataBeans.size() % Constant.PAGE_SIZE == 0 ? true : false;
                                        ((IndexFragment) (Nav3Fragment.this.getParentFragment())).getIsLoadMoreArray()[2] = isLoadMore;

                                        //当是第一页的时候先清空数组，防止下拉操作的时候加载重复数据
                                        if (currentPage == 1) {
                                            myRecordReportAdapter.replaceData(dataBeans);
                                        }else{
                                            myRecordReportAdapter.addData(dataBeans);
                                        }
                                        ((IndexFragment) (Nav3Fragment.this.getParentFragment())).setIndexReportDataBean((LinkedList<MyReportRecordBean.PageBean.ListBean>) myRecordReportAdapter.getData());
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                                isLoadMore = false;
                                ToastUtils.showLong(getResources().getString(com.mingtu.center.R.string.text_json_error));
                            }

                        }
                    }
                });

    }

    /**
     * 刷新数据
     */
    public void refresh() {
        getReportList(1);
    }
}